package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.HeadStaffDto;
import com.dingreading.cloud.admin.entity.HeadStaff;
import com.dingreading.cloud.admin.entity.table.HeadStaffTableDef;
import com.dingreading.cloud.admin.mapper.HeadStaffMapper;
import com.dingreading.cloud.admin.service.HeadStaffService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.AdminRoleStaffTableDef.adminRoleStaff;


/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class HeadStaffServiceImpl extends BaseServiceImpl<HeadStaffMapper, HeadStaff, HeadStaffTableDef> implements HeadStaffService {

    @Override
    protected HeadStaffTableDef getTable() {
        return HeadStaffTableDef.headStaff;
    }

    @Override
    public Page<HeadStaffDto> pageList(PageUtil pageUtil, Integer enabled, String phone, String userName) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (StringUtils.isNotBlank(phone))
            condition.and(table.phone.like(phone));
        if (StringUtils.isNotBlank(userName))
            condition.and(table.userName.like(userName));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, HeadStaffDto.class);
    }

    @Override
    public HeadStaffDto getByPhone(String phone) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.phone.eq(phone));
        return getMapper().selectOneByQueryAs(wrapper, HeadStaffDto.class);
    }

    @Override
    public void updateStatus(Integer status, List<Long> idList) {
        HeadStaff data = UpdateEntity.of(HeadStaff.class);

        UpdateWrapper<HeadStaff> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.enabled, status);

        getMapper().updateByCondition(data, table.id.in(idList));
    }

    @Override
    public List<HeadStaff> getUseList() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(table.enabled.eq(1))
                .orderBy(table.id.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public HeadStaff getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public HeadStaffDto dtoGetByUid(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        return getMapper().selectOneByQueryAs(wrapper, HeadStaffDto.class);
    }

    @Override
    public List<HeadStaff> getByUids(List<String> uid) {
        return getMapper().selectListByCondition(table.uid.in(uid));
    }

    @Override
    public List<HeadStaff> getByRoleId(Long roleId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.userName, table.phone)
                .from(table)
                .leftJoin(adminRoleStaff).on(table.uid.eq(adminRoleStaff.headStaffUid))
                .where(adminRoleStaff.roleId.eq(roleId));

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<HeadStaff> getByRoleIdIn(List<Long> roleIds) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.userName, table.phone)
                .from(table)
                .leftJoin(adminRoleStaff).on(table.uid.eq(adminRoleStaff.headStaffUid))
                .where(adminRoleStaff.roleId.in(roleIds).and(table.enabled.eq(1)))
                .groupBy(table.uid);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean havePhone(Long id, String phone) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (id != null)
            condition.and(table.id.ne(id));
        condition.and(table.phone.eq(phone));
        return exists(condition);
    }

    @Override
    public boolean updateLoginInfo(String staffUid, String token, String loginTime) {
        return UpdateChain.of(HeadStaff.class)
                .set(table.lastLoginTime, loginTime)
                .set(table.token, token)
                .where(table.uid.eq(staffUid))
                .update();
    }


}
